package com.example.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.bean.CatBean;
import com.example.home.park.CarParkDetailPageActivity;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class CatcomitAdapter extends RecyclerView.Adapter<CatcomitAdapter.MyHolder> {
    private Context context;
    private List<CatBean.RowsBean> list;
    private boolean showAllData = false; // 用于控制是否显示所有数据

    public void setShowAllData(boolean showAllData) {
        this.showAllData = showAllData;
        notifyDataSetChanged(); // 刷新 RecyclerView 以显示所有数据
    }


    public CatcomitAdapter(Context context, List<CatBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cat, null);
        return new MyHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        Glide.with(context).load(ip + list.get(position).getImgUrl()).into(holder.car_img);
        Log.e("ipa", ip + list);
        holder.cat_name.setText("停车场名:" + list.get(position).getParkName());
        holder.car_adress.setText("停车场地址:" + String.valueOf(list.get(position).getAddress()));
        holder.cat_num.setText("空位数量:" + String.valueOf(list.get(position).getVacancy()));
        holder.cat_price.setText("收费价格:" + String.valueOf(list.get(position).getRates()) + "元");
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CarParkDetailPageActivity.class);
                intent.putExtra("listID", String.valueOf(list.get(position).getId()));
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return showAllData ? list.size() : Math.min(list.size(), 5);
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView car_img;
        TextView cat_name;
        TextView car_adress;
        TextView cat_num;
        TextView cat_price;
        RelativeLayout rootView;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            car_img = itemView.findViewById(R.id.car_img);
            cat_name = itemView.findViewById(R.id.cat_name);
            car_adress = itemView.findViewById(R.id.car_adress);
            cat_num = itemView.findViewById(R.id.cat_num);
            cat_price = itemView.findViewById(R.id.cat_price);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
